using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Models;
using Models.ServerModels;
using System.Web;

namespace DdnsServer.Controllers
{
    [RoutePrefix("Ddns")]
    public class DdnsController : ApiController
    {
        private BLL.UserBLL uCtrl = new BLL.UserBLL();
        private BLL.RecordBLL rCtrl = new BLL.RecordBLL();

        [Route("UpdateDomain")]
        public IHttpActionResult UpdateDomain([FromBody] Dictionary<string, object> dc)
        {
            try
            {
                if (null == dc)
                    throw new Exception("参数不能为空！");
                string userId = dc.Keys.Contains("userId") ? dc["userId"].ToString() : "";
                if (string.IsNullOrWhiteSpace(userId))
                    throw new Exception("用户ID不能为空！");
                string ip = Comm.GetClientIP();
                rCtrl.UpdateRecord(userId, ip);
                return Ok(new RetModel { ErrCode = 0, Success = true, Message = "UID:" + userId + " IP:" + ip });
            }
            catch (Exception ex)
            {
                return Ok(new RetModel { Data = null, ErrCode = 1, Message = ex.Message, Success = false });
            }
        }

        [Route("GetIP"), HttpGet]
        public IHttpActionResult GetIP()
        {
            return Ok(Comm.GetClientIP());
        }
    }
}